/**
 * 
 */
package com.youxin.cpcenter.common.dbcache;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.youxin.busfoundation.bean.classify.RequestCityBean;
import com.youxin.busfoundation.bean.classify.RequestClassifyBean;
import com.youxin.busfoundation.bean.classify.ResponseCityBean;
import com.youxin.busfoundation.bean.classify.ResponseClassifyBean;
import com.youxin.cpcenter.service.ClassifyService;
import com.youxin.foundation.utils.StringUtils;
import com.youxin.foundation.utils.thread.BaseThreadPool;
import com.youxin.foundation.utils.thread.ScheduleThreadPool;
import com.youxin.foundation.utils.thread.SingleThreadPool;

/**
 * @author yingde.cao
 * @date 2017年12月16日
 * @version 1.0
 */
public class CacheDataObject {
	
	public List<ResponseCityBean> totalCitys=null;
	public List<ResponseClassifyBean> totalClassifys=null;
	
	private List<ResponseClassifyBean> classifyBeans = null;
	private List<ResponseCityBean> cityBeans = null;
	private final static CacheDataObject cacheObject = new CacheDataObject();

	public static CacheDataObject instance() {
		return cacheObject;
	}
	
//	public boolean isCity(ResponseCityBean bean)
//	{
//		int id=bean.getRegionId();
//		return id==110000||id==120000||id==310000||id==
//	}

	/**
	 * 加载初始数据
	 * 
	 * @description
	 * @param classifyService
	 */
	public void loadService(ClassifyService classifyService) {
		/**
		 * 加载城市
		 */		
		try {
			if(totalCitys==null)
			{
				List<ResponseCityBean> tempList = null;
				try {
					tempList = classifyService.listTotalCitys();
					
				} catch (Exception e) {
				}
				synchronized (this) {
					if (totalCitys == null && tempList != null) {
						totalCitys = tempList;
						tempList = classifyService.listCity(totalCitys);
						cityBeans=tempList;
//						cityBeans = tempList.stream().filter((t)->
//						
//						);
					
						
						
						regionMap = new HashMap<>(totalCitys.size());
						for (ResponseCityBean item : totalCitys) {
							regionMap.put(item.getRegionId(), item.getRegionName());
						}
					}
				}
			}
		} catch (Exception e) {
		}
		
		try {
			if(totalClassifys==null)
			{
				List<ResponseClassifyBean> tempList = null;
				try {
					tempList = classifyService.listTotalIndustrys();
				} catch (Exception e) {
				}
				synchronized (this) {
					if (totalClassifys == null && tempList != null) {
						totalClassifys = tempList;
						tempList = classifyService.listIndustrys(totalClassifys);
						classifyBeans = tempList;
						
						classifyMap = new HashMap<>(totalClassifys.size());
						for (ResponseClassifyBean item : totalClassifys) {
							classifyMap.put(String.valueOf(item.getUid()), item.getName());
						}
						
					}
				}
			}
		} catch (Exception e) {
		}

		
	}

	public void refreshCache() {
		ScheduleThreadPool.schedule(new Runnable() {
			@Override
			public void run() {
				try {
					/**
					 * 清除缓存
					 */
					clearCache();
				} catch (Exception e) {
				}

			}
		}, 60000);
	}

	public void clearCache() {
		try {
			synchronized (this) {
				if(totalCitys!=null)
				{
					totalCitys.clear();
					totalCitys=null;
				}
				
				if(totalClassifys!=null)
				{
					totalClassifys.clear();
					totalClassifys=null;
				}
				
				if (classifyBeans != null) {
					classifyBeans.clear();
					classifyBeans = null;
				}
				
				if (cityBeans != null) {
					cityBeans.clear();
					cityBeans = null;
				}
				
				if (classifyMap != null) {
					classifyMap.clear();
					classifyMap = null;
				}
				
				if (regionMap != null) {
					regionMap.clear();
					regionMap = null;
				}
			}
		} catch (Exception e) {
		}

	}

	/**
	 * 获取分类信息
	 * 
	 * @description
	 * @return
	 */
	public List<ResponseClassifyBean> getClassifyBeans(ClassifyService classifyService) {
		if (classifyBeans == null) {
			loadService(classifyService);
		}
		synchronized (this) {
			if (classifyBeans == null) {
				return null;
			} else {
				return new ArrayList<>(classifyBeans);
			}
		}
	}

	/**
	 * 获取城市信息
	 * 
	 * @description
	 * @return
	 */
	public List<ResponseCityBean> getCityBeans(ClassifyService classifyService) {
		if (cityBeans == null) {
			loadService(classifyService);
		}
		synchronized (this) {
			if (cityBeans == null) {
				return null;
			} else {
				return new ArrayList<>(cityBeans);
			}
		}
	}

	public static CacheDataObject getCacheobject() {
		return cacheObject;
	}

	private Map<String, String> classifyMap = null;
	private Map<Integer, String> regionMap = null;

//	public Map<Integer, String> getClassifyMap() {
//		return classifyMap;
//	}
//
//	public Map<Integer, String> getRegionMap() {
//		return regionMap;
//	}


	/**
	 * 获取分类信息
	 * 
	 * @description
	 * @param ids
	 * @param classifyService
	 * @return
	 */
	public List<String> getIndustryName(List<String> ids, ClassifyService classifyService) {
		if (ids == null) {
			return new ArrayList<>();
		}
		loadService(classifyService);
		List<String> results = new ArrayList<>(ids.size());
		for (String item : ids) {
			String name = null;			
			synchronized (this) {
				name = classifyMap.get(item);
			}
			try {
				results.add(StringUtils.isNullSetEmpty(name));
			} catch (Exception e) {
				results.add("");
			}
		}
		return results;
	}

	/**
	 * 获取城市列表
	 * 
	 * @description
	 * @param ids
	 * @param classifyService
	 * @return
	 */
	public List<String> getRegionNames(List<Integer> ids, ClassifyService classifyService) {
		if (ids == null) {
			return new ArrayList<>();
		}
		loadService(classifyService);
		List<String> results = new ArrayList<>(ids.size());
		for (Integer item : ids) {
			String name = null;
			synchronized (this) {
				name = regionMap.get(item);
			}
			try {
				results.add(StringUtils.isNullSetEmpty(name));
			} catch (Exception e) {
				results.add("");
			}
		}
		return results;
	}

}
